﻿using Nasty.Core.LoadParams;
using Nasty.Core.Repository;
using Nasty.Core.SuperExtension;
using Nasty.FlowModule.Areas.FlowConfig.Model;
using Nasty.FlowModule.Areas.FlowConfig.Params;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;

namespace Nasty.FlowModule.FlowConfig
{
    public interface IFlowConfigRepository : IRepository<Nasty.Core.Entity.Flow.FlowConfig>
    {
        public Nasty.Core.Entity.Flow.FlowConfig GetFlowConfig(string id);

        public ResultData<Nasty.Core.Entity.Flow.FlowConfig> SaveFlowConfig(FlowConfigModel model);

        public ResultData<string> DeleteFlowConfigs(List<string> ids);

        public PageData<Nasty.Core.Entity.Flow.FlowConfig> GetFlowConfigPage(GetFlowConfigPageParams @params);

        public List<Nasty.Core.Entity.Flow.FlowConfig> GetFlowConfigs(GetFlowConfigsParams @params);

        public ResultData<string> SwitchFlowConfigPublish(SwitchFlowConfigPublishParams @params);

        public ResultData<string> SaveFlowConfigNodes(BatchParams<FlowConfigNodeModel> models);

        public List<Nasty.Core.Entity.Flow.FlowConfigNode> GetFlowConfigNodes(string configId);
    }

    public class FlowConfigRepository : SqlRepository<Nasty.Core.Entity.Flow.FlowConfig>, IFlowConfigRepository
    {
        public FlowConfigRepository(SqlSugarClient db) : base(db)
        {
            //db.CodeFirst.InitTables(typeof(FlowConfigNode));
        }

        public ResultData<string> DeleteFlowConfigs(List<string> ids)
        {
            var result = new ResultData<string>();
            try
            {
                var configs = Db.Queryable<Nasty.Core.Entity.Flow.FlowConfig>().In(ids).ToList();
                foreach (var config in configs)
                {
                    Db.Delete(config);
                }

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public Nasty.Core.Entity.Flow.FlowConfig GetFlowConfig(string id)
        {
            return this.Db.Queryable<Nasty.Core.Entity.Flow.FlowConfig>().IncludesAllFirstLayer().InSingle(id);
        }

        public PageData<Nasty.Core.Entity.Flow.FlowConfig> GetFlowConfigPage(GetFlowConfigPageParams @params)
        {
            int totalPage = 0;
            int total = 0;
            var pageData = new PageData<Nasty.Core.Entity.Flow.FlowConfig>();

            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.Flow.FlowConfig>();

            if (!string.IsNullOrEmpty(@params.Name)) _SQLExpress.Where((t) => t.Name.Contains(@params.Name));
            if (!string.IsNullOrEmpty(@params.Code)) _SQLExpress.Where((t) => t.Code.Contains(@params.Code));

            _SQLExpress = _SQLExpress.OrderBy((t) => t.CreateTime, OrderByType.Desc);

            var data = _SQLExpress.ToPageList(@params.Current, @params.PageSize, ref total, ref totalPage);

            pageData.Total = total;
            pageData.TotalPage = totalPage;
            pageData.Data = data;

            pageData.Current = @params.Current;
            pageData.PageSize = @params.PageSize;
            return pageData;
        }

        public List<Nasty.Core.Entity.Flow.FlowConfig> GetFlowConfigs(GetFlowConfigsParams @params)
        {
            var _SQLExpress = Db.Queryable<Nasty.Core.Entity.Flow.FlowConfig>();
            if (!string.IsNullOrEmpty(@params.Name)) _SQLExpress.Where((t) => t.Name.Contains(@params.Name));
            if (!string.IsNullOrEmpty(@params.Code)) _SQLExpress.Where((t) => t.Code.Contains(@params.Code));

            return _SQLExpress.ToList();
        }

        public ResultData<Nasty.Core.Entity.Flow.FlowConfig> SaveFlowConfig(FlowConfigModel model)
        {
            var result = new ResultData<Nasty.Core.Entity.Flow.FlowConfig>();
            try
            {
                var data = Db.Save<Nasty.Core.Entity.Flow.FlowConfig>(model);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public ResultData<string> SaveFlowConfigNodes(BatchParams<FlowConfigNodeModel> models)
        {
            var result = new ResultData<string>();
            try
            {
                Db.BeginTran();
                var config = Db.Queryable<Nasty.Core.Entity.Flow.FlowConfig>().InSingle(models.Id);
                if (config == null) throw new Exception("流程配置不存在");
                Db.Update(config);

                Db.Deleteable<Nasty.Core.Entity.Flow.FlowConfigNode>().Where((t) => t.FlowConfigId == models.Id).ExecuteCommand();

                foreach (var model in models.Items)
                {
                    Db.Save<Nasty.Core.Entity.Flow.FlowConfigNode>(model);
                }
                Db.CommitTran();
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                Db.RollbackTran();
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

        public List<Nasty.Core.Entity.Flow.FlowConfigNode> GetFlowConfigNodes(string configId)
        {
            return Db.Queryable<Nasty.Core.Entity.Flow.FlowConfigNode>().Where((t) => t.FlowConfigId == configId).IncludesAllFirstLayer().ToList();
        }

        public ResultData<string> SwitchFlowConfigPublish(SwitchFlowConfigPublishParams @params)
        {
            var result = new ResultData<string>();
            try
            {
                var config = Db.Queryable<Nasty.Core.Entity.Flow.FlowConfig>().InSingle(@params.Id);
                if (config == null)
                {
                    result.IsSuccess = false;
                    result.Message = "流程配置不存在";
                    return result;
                }
                config.IsPublish = @params.IsPublish;
                Db.Update(config);
                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
                return result;
            }
        }

    }
}
